package com.shop.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.BulkGoodsDetail;
import com.shop.entity.BulkMerchant;
import com.shop.entity.MerchantMerchant;
import com.shop.entity.SupplierSupplier;
import com.shop.mapper.SupplierOrderMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@AllArgsConstructor
public class SupplierOrderService {
    private final SupplierOrderMapper orderMapper;

    public Rsp searchSomeOrderPage(Integer id, PageParam<BulkMerchant> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<BulkMerchant> list = orderMapper.getBySupplierId(id);
        return Rsp.success(new PageInfo<>(list));
    }

    public Rsp searchSomeOrderDetailPage(PageParam<BulkGoodsDetail> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<BulkGoodsDetail> list = orderMapper.getByOrderId(pageParam.getModel().getBulkId());
        return Rsp.success(new PageInfo<>(list));
    }

    public Rsp outWarehouse(BulkMerchant bulkMerchant) {
        orderMapper.outWarehouse(bulkMerchant);
        return Rsp.success(null);
    }

    //下订单
    public Rsp addOrder(Integer userId, BulkMerchant bulkMerchant) {
        //根据id查询商户详情
        MerchantMerchant merchant = orderMapper.getUserInfo(userId);
        //给订单设置当时的属性
        bulkMerchant.setMerchantId(merchant.getId());
        bulkMerchant.setMerchantName(merchant.getMerchantName());
        bulkMerchant.setMerchantPhone(merchant.getMerchantPhone());
        bulkMerchant.setMerchantAddress(merchant.getMerchantAddress());
        bulkMerchant.setCount(bulkMerchant.getDetailList().size());
        bulkMerchant.setShippingTime(new Date());
        bulkMerchant.setStatus("0");
        //根据供货商id查供货商
        SupplierSupplier supplier = orderMapper.getSupplier(bulkMerchant.getSupplyId());
        bulkMerchant.setSupplyName(supplier.getName());
        bulkMerchant.setSupplyPhone(supplier.getContactPhone());
        bulkMerchant.setSupplyAccount(supplier.getAccount());
        //设置订单号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        bulkMerchant.setOrderNo("NO" + format + UUID.randomUUID().toString().substring(0, 4));
        //主订单加入数据库
        int i = orderMapper.insertSelective(bulkMerchant);
        Integer autoId = bulkMerchant.getId();
        //遍历订单中的list加入数据库
        for (BulkGoodsDetail detail : bulkMerchant.getDetailList()) {
            detail.setCreateUserId(userId);
            detail.setBulkId(autoId);
            int res = orderMapper.insertDetail(detail);
            if (res <= 0) {
                return Rsp.fail();
            }
        }

        if (i > 0) {
            return Rsp.success(i).msg("下单成功");
        } else {
            return Rsp.fail();
        }

    }
}
